package com.desire.test;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;

/**
 * @author desire
 */
public class _20_序列化测试 {
    public static void main(String[] args) {
        //write();
        try (var ois = new ObjectInputStream(new FileInputStream("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bank.dat"))) {

            var a = ois.readObject();
            System.out.println(a.toString());

        } catch (FileNotFoundException e) {
            System.out.println("没有bank.dat文件");
        } catch (IOException e) {
            System.out.println("数据读取完成");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void write() {
        try (var oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\desire\\Desktop\\javaProject\\01-javaBase\\src\\main\\resources\\bank.dat", true))) {
            var u = new user(1, "刻晴", 12);
            // java.io.NotSerializableException: com.desire.test.user  需要实现序列化
            oos.writeObject(u);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class user implements Serializable {
    private int id;
    private String name;
    private int age;
}
